<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/08/06
 * Time: 10:20
 * Description: C端3.0 用户模块
 */

class ApiUserController extends  BaseController {

    /**
     * @Author:      sqq
     * @DateTime:    2018-08-06 10:20:29
     * @Description: 用户详情
     * @link:         http://www.activity.com/wechat/api3.0/user/show?token=668c155fab8dd6ef1e789b33ff6d040e&id=1000037858
     */
    public function show(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id'=>'required', //用户id
            )
        );

        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $wechat_user=$this->verificationUserToken(Input::get("token"),$response);
            if(is_null($wechat_user)){
                break;
            }

            $id = Input::get('id');
            $user = Users::where('id',$id)->first();
            if(!empty($user)){
                $user->heading_img_url = Config::get('config.heading_img_url');

                if($user->birthday == "0000-00-00"){
                    $user->birthday = "";
                }

                //查询会员等级
                $user->o_member = MemberOrdinary::find($user->member);
                //查询是否是特殊会员
                $now = Common::currentTimeString();
                $user->t_member = MemberTemporaryUser::from('member_temporary_user as mtu')->leftJoin('member_temporary as mt','mt.id','=','mtu.member_temporary_id')
                    ->where('mtu.user_id',$user->id)->where('mtu.get_time','<=',$now)->where('mtu.status',1)->where('mtu.end_time','>=',$now)->select('mt.*')->get();
            }

            //组织返回
            $response = array(
                'code' => 0,'msg' => 'ok','result' => array('user' => $user,)
            );

        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "show", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-08-06 14:05:29
     * @Description: 车辆详情
     * @link: http://www.activity.com/wechat/api3.0/user/car_show?token=668c155fab8dd6ef1e789b33ff6d040e&id=345787
     */
    public function car_show(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id'=>'required', //车辆id
            )
        );

        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $wechat_user=$this->verificationUserToken(Input::get("token"),$response);
            if(is_null($wechat_user)){
                break;
            }

            $id = Input::get('id');
            $car = UserCarInfo::with('user_car_info_inquiry')->where('id',$id)->where('user_id',$wechat_user->id)->first();
            if(!empty($car)){

                //获取车型信息
                $car->car_message = UserCarInfo::getCarStandardInfoWechat3($car->car_model_id);
                $car->car_message->car_brand_img_url = Config::get('config.car_brand_img_url');

                //查询车辆检测报告
                //$car->checkreport = Orders::join('order_detection','orders.order_id','=','order_detection.order_id')->where('orders.user_car_id',$id)->orderBy('order_detection.updated_at','desc')->whereIn('orders.status',[1117,1317,1018,1217,1417])->select('orders.order_id')->first();

                $orders = Order30Check::getMileage($id,$wechat_user->id,'1117,1317,1018,1217,1219,1417,1419,1420');
                //$orders = Order30Check::getMileage($id,$wechat_user->id,'1117,1317,1018,1217,1417');
                $check_report_arr = $temp_check_report_arr = array();
                foreach($orders as $_o){
                    $order_30_check = OrderDetection::where('order_id',$_o->order_id)->get()->toArray();
                    //如果查询数据为空  忽略词条数据，挑出本次循环，继续循环
                    if(empty($order_30_check)) continue;
                    if(count($order_30_check) > 0){
                        $temp_check_report_arr['check_data'] = date('Y-m-d',strtotime($order_30_check[0]['created_at']));
                    }
                    $temp_check_report_arr['place'] = $_o->place;
                    $temp_check_report_arr['order_id'] = $_o->order_id;
                    $temp_check_report_arr['mileage'] = $_o->mileage;
                    $temp_check_report_arr['shop_id'] = $_o->shop_id;
                    $check_report_arr[] = $temp_check_report_arr;
                }
                $car->checkreport = $check_report_arr;

                //判断该车辆是否添加服务在购物车
                $car->shopping = Shopping::where('car_model_id',$car->car_model_id)->where('car_brand_id',$car->car_brand_id)->where('car_series_id',$car->car_series_id)->where('status',1)->where('type',2)->where('user_id',$wechat_user->id)->count();
            }else{
                $response = Lang::get("error.user_car_not_exit_error_1019");
                break;
            }

            //组织返回
            $response = array(
                'code' => 0,'msg' => 'ok','result' => array('user' => $car)
            );

        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "show", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    public function is_checkreport($order_id){
        $order_detection = OrderDetection::where('order_id',$order_id)->first();
        $of = OrderFlows::where('order_id',$order_id)->select('status')->get()->toArray();
        $tmp1 = array();
        foreach($of as $_o){
            $tmp1[] = $_o['status'];
        }
        $status = array('status' => 2, 'report' => '', 'time' => '');//服务完成  检测中
        if(!is_null($order_detection) && !in_array('1419',$tmp1)){
            $status = array('status' => 3, 'report' => $order_detection, 'time' => $order_detection->updated_at);//有检测报告 显示分数  //检测完成
        }
        if(is_null($order_detection)){
            $status = array('status' => 1, 'report' => '', 'time' => '');  //没有检测报告  //没有检测报告
        }
        return $status;
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-08-06 15:32:28
     * @Description: 车库列表
     * @param  $token  String  用户token
     * @link   http://www.activity.com/wechat/api3.0/user/garage?token=668c155fab8dd6ef1e789b33ff6d040e
     */
    public function Garage(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $users=$this->verificationUserToken(Input::get("token"),$response);
            if(is_null($users)){
                break;
            }

            //根据用户id获取用户的所有车辆（优先展示默认车辆）[排序方式：默认 > 曾到店 > id]
            $re = UserCarInfo::getUserCarInfo($users->id);

            $response = array('code' => 0, 'msg' => 'ok', 'result' => $re);
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "Garage", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      sqq
     * @DateTime:    2018-08-08 13:56:53
     * @Description: 我的信息
     * @link：http://www.activity.com/wechat/api3.0/user/myInformation?token=668c155fab8dd6ef1e789b33ff6d040e
     */
    public function myInformation(){
        $validator = Validator::make(Input::all(),
            array(
                "token" => "required",
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $wechat_user=$this->verificationUserToken(Input::get("token"),$response);
            if(is_null($wechat_user)){
                break;
            }
            //获取参数
            $order_type = Input::get('order_type',1);
            //未读消息累加
            $is_read_count = NewsLog::where('is_read',1)->where('type','!=',6)->where('field_name','user_id')->where('field_value',$wechat_user->id)->where('status',NewsLog::STATUS_NEW)->count();
            $order_is_read_count = NewsLog::where('is_read',1)->where('field_name','user_id')->where('type',1)->where('field_value',$wechat_user->id)->where('status',NewsLog::STATUS_NEW)->count();//1-订单
            $coupon_is_read_count = NewsLog::where('is_read',1)->where('field_name','user_id')->where('type',2)->where('field_value',$wechat_user->id)->where('status',NewsLog::STATUS_NEW)->count();//2-优惠券
            $feed_is_read_count = NewsLog::where('is_read',1)->where('field_name','user_id')->where('type',3)->where('field_value',$wechat_user->id)->where('status',NewsLog::STATUS_NEW)->count();//3-反馈
            $money_is_read_count = NewsLog::where('is_read',1)->where('field_name','user_id')->where('type',4)->where('field_value',$wechat_user->id)->where('status',NewsLog::STATUS_NEW)->count();//4-余额
            $card_is_read_count = NewsLog::where('is_read',1)->where('field_name','user_id')->where('type',5)->where('field_value',$wechat_user->id)->where('status',NewsLog::STATUS_NEW)->count();//5-套餐卡

            $user_account = UserAccount::where('user_id',$wechat_user->id)->first();
//            $to_be_confirmed = Orders::where('user_id',$wechat_user->id)->whereIn('status',Orders::$order_status[2])->where('order_type',1)->count();
//            $confirmed = Orders::where('user_id',$wechat_user->id)->whereIn('status',Orders::$order_status[3])->where('order_type',1)->count();

            //查询服务单待评价的订单总数
            $wait_evaluate_service = Orders::where('user_id',$wechat_user->id)->whereIn('status',Orders::$order_status_wechat3[5])->where('order_type',1)->count();
            //查询洗车单待评价的订单总数
            $wait_evaluate_wash = CarWashOrder::where('user_id',$wechat_user->id)->whereIn('status',Orders::$order_status_wechat3[5])->count();
            //查询零售单待评价的订单总数
            $wait_evaluate_retail = Orders::where('user_id',$wechat_user->id)->whereIn('status',Orders::$order_status_wechat3[5])->where('order_type',2)->count();

            //查询服务单服务中的订单总数
            $service_in_service = Orders::where('user_id',$wechat_user->id)->whereIn('status',Orders::$order_status_wechat3[4])->where('order_type',1)->count();
            //查询洗车单服务中的订单总数
            $wash_in_service = CarWashOrder::where('user_id',$wechat_user->id)->whereIn('status',Orders::$order_status_wechat3[4])->count();
            //查询零售单服务中的订单总数
            $retail_in_service = Orders::where('user_id',$wechat_user->id)->whereIn('status',Orders::$order_status_wechat3[4])->where('order_type',2)->count();

            $coupons = Coupons::getCouponsList($wechat_user->id,2);
            $coupons_count = count($coupons);
            if(!is_null($coupons)){
                foreach($coupons as $v) {
                    $coupon_base_info = CouponBaseInfo::getCouponBaseInfo($v->coupon_base_info_id);
                    if(empty($coupon_base_info)){
                        $coupons_count = $coupons_count-1;
                    }else{
                        if($coupon_base_info->date_type == 1){
                            $end_date = $v->end_date;
                        }else{
                            $end_date = $coupon_base_info->end_date;
                        }
                        if($v->status === 2 && strtotime(Common::currentTimeString()) - strtotime($end_date.' 23:59:59') > 0){
                            $coupons_count = $coupons_count-1;
                        }
                    }
                }
            }


            $response = array(
                "code" => 0,
                "msg" => "ok",
                "result" => array(
                    "remaining_sum" => isset($user_account->balance)?bcdiv($user_account->balance,100,2):'0.00',
                    //"to_be_confirmed" => $to_be_confirmed,
                    //"confirmed" => $confirmed,
                    "coupons_count" => $coupons_count,
                    "wait_evaluate_service" => $wait_evaluate_service,
                    "wait_evaluate_wash" => $wait_evaluate_wash,
                    "wait_evaluate_retail" => $wait_evaluate_retail,
                    "is_read_count" => $is_read_count,
                    "coupon_is_read_count" => $coupon_is_read_count,
                    "feed_is_read_count" => $feed_is_read_count,
                    "money_is_read_count" => $money_is_read_count,
                    "card_is_read_count" => $card_is_read_count,
                    "order_is_read_count" => $order_is_read_count,
                    "service_in_service" => $service_in_service,
                    "wash_in_service" => $wash_in_service,
                    "retail_in_service" => $retail_in_service,
                )
            );
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "myInformation", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-08-13 15:14:53
     * @Description: 账户余额
     * @link:
     */
    public function remainingSum(){
        $validator = Validator::make(Input::all(),
            array(
                "token" => "required",
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $wechat_user=$this->verificationUserToken(Input::get("token"),$response);
            if(is_null($wechat_user)){
                break;
            }
            $user_account = UserAccount::where('user_id',$wechat_user->id)->first();
            $remaining_sum = isset($user_account->balance)?bcdiv($user_account->balance,100,2):'0.00';
            $recharge_record = DB::table('acount_user_recharge_record')
                ->select([
                    'amount',
                    'created_at',
                    'pay_type'
                ])
                ->where('user_id',$wechat_user->id)
                ->whereIn('status',array(1,6))
                ->get();
            foreach($recharge_record as $k => $v){
                $v->type = AcountUserRecharge::$pay_type_name[$v->pay_type];
                $v->record_type = 1;
                $v->is_wash = 0;
                $v->amount = "+".bcdiv($v->amount,100,2);
            }
            $spend_record = DB::table('acount_user_recharge_spend_record')
                ->select([
                    'type',
                    'amount',
                    'tran_no',
                    'created_at',
                    'buss_id'
                ])
                ->where("user_id",$wechat_user->id)
                ->whereIn('type',array(1,2,3,4,5,6,7,8))
                ->where('status',1)
                ->get();
            foreach($spend_record as $k => $v){
                if($v->type == 1){
                    $pay_record = OrderPayRecord::find($v->buss_id);
                    if(!empty($pay_record)){
                        $v->record_type = 2;
                        $v->type = "订单消费";
                        $orders = Orders::where('order_id',$pay_record->order_id)->first();
                        if($orders->order_type == 1){
                            $v->is_wash = 0;
                        }else{
                            $v->is_wash = 2;
                        }
                        $v->order_id = $pay_record->order_id;
                        $v->amount = "-".bcdiv($v->amount,100,2);
                        array_push($recharge_record,$v);
                    }
                }
                if(in_array($v->type,array(3,4,5,6,7,8))){
                    $v->record_type = 2;
                    $v->is_wash = 0;
                    if($v->type == 8){
                        $pay_record = ServiceCardOrderPayRecord::find($v->buss_id);
                        if(!empty($pay_record)){
                            $orders = ServiceCardOrder::where('order_id',$pay_record->order_id)->first();
                            if(!empty($orders)){
                                $v->order_id = $pay_record->order_id;
                            }
                            $v->is_wash = 4;  //套餐卡
                        }
                    }
                    $v->type = AcountUserRecharge::$recharge_type[$v->type];
                    $v->amount = "-".bcdiv($v->amount,100,2);
                    array_push($recharge_record,$v);
                }
//                if($v->type == 3){
//                    $v->record_type = 2;
//                    $v->type = "撤销充值消费";
//                    $v->is_wash = 0;
//                    $v->amount = "-".bcdiv($v->amount,100,2);
//                    array_push($recharge_record,$v);
//                }
                if($v->type == 2){
                    $pay_record = CarWashOrderPayRecord::find($v->buss_id);
                    $v->record_type = 2;
                    $v->type = "洗车单消费";
                    $v->is_wash = 1;
                    $v->order_id = $pay_record->order_id;
                    $v->amount = "-".bcdiv($v->amount,100,2);
                    array_push($recharge_record,$v);
                }
//                if($
//                if($v->type == 4){
//                    $v->record_type = 2;
//                    $v->type = "退款消费";
//                    $v->is_wash = 0;
//                    $v->amount = "-".bcdiv($v->amount,100,2);
//                    array_push($recharge_record,$v);
//                }
//                if($v->type == 6){
//                    $v->record_type = 2;
//                    $v->type = "余额调整消费";
//                    $v->is_wash = 0;
//                    $v->amount = "-".bcdiv($v->amount,100,2);
//                    array_push($recharge_record,$v);
//                }
            }
            $wasorder_record = ReturnOrderRecord::join('orders','orders.order_id','=','return_order_record.return_order_id')
                ->select([
                    'return_order_record.return_order_id as order_id',
                    'return_order_record.amount',
                    'return_order_record.created_at'
                ])
                ->where('orders.user_id',$wechat_user->id)
                ->where("return_order_record.pay_type",8)
                ->get();
            foreach($wasorder_record as $k => $v){
                $v->type = "退款";
                $v->is_wash = 0;
                $v->record_type = 1;
                $v->amount = "+".bcdiv($v->amount,100,2);
                array_push($recharge_record,$v);
            }
            $spendre_record = AcountUserRechargeSpendRe::select([
                'type',
                'amount',
                'tran_no',
                'created_at'
            ])
                ->where("user_id",$wechat_user->id)
                ->where('status',1)
                ->get();
            foreach($spendre_record as $v){
//                if($v->type == 1){
//                    $v->record_type = 1;
//                    $v->type = "订单退款";
//                    $v->is_wash = 0;
//                    $v->amount = "+".bcdiv($v->amount,100,2);
//                    array_push($recharge_record,$v);
//                }
                if($v->type == 2){
                    $v->record_type = 1;
                    $v->type = "洗车单退款";
                    $v->is_wash = 1;
                    $v->amount = "+".bcdiv($v->amount,100,2);
                    array_push($recharge_record,$v);
                }
                if(in_array($v->type,array(1,3,4))){
                    $v->record_type = 1;
                    $v->type = AcountUserRecharge::$rechargere_type[$v->type];
                    $v->is_wash = 0;
                    $v->amount = "+".bcdiv($v->amount,100,2);
                    array_push($recharge_record,$v);
                }
                if(in_array($v->type,array(5,7))){
                    $v->record_type = 1;
                    $v->type = '消费退款';
                    $v->is_wash = 0;
                    $v->amount = "+".bcdiv($v->amount,100,2);
                    array_push($recharge_record,$v);
                }
            }
            $data = json_decode(json_encode($recharge_record),true);
            $sorts = array(
                'direction' => "SORT_DESC", //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
                'field'     => "created_at",       //排序字段
            );
            if(!empty($data)){
                $arrSort = array();
                foreach($data AS $uniqid => $row){
                    foreach($row AS $key=>$value){
                        $arrSort[$key][$uniqid] = $value;
                    }
                }
                if($sorts['direction']){
                    array_multisort($arrSort[$sorts['field']], constant($sorts['direction']), $data);
                }
            }
            $response = array(
                "code" => 0,
                "msg" => "ok",
                "result" => array(
                    'remaining_sum' => $remaining_sum,
                    'record' => $data,
                )
            );
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "remainingSum", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-08-14 17:49:53
     * @Description: 跟换微信头像
     * @link:   http://www.activity.com/wechat/api3.0/user/change_heading_img?token=301fa556cf7509819b36db43fb4df384&user_id=1000037858&type=1
     */
    public function change_heading_img(){
        $validator = Validator::make(Input::all(),
            array(
                "token" => "required",
                "user_id" => "required",
                "type" => "required",
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $wechat_user=$this->verificationUserToken(Input::get("token"),$response);
            if(is_null($wechat_user)){
                break;
            }

            //获取参数
            $user_id = Input::get('user_id');
            $type = Input::get('type',1);

            //获取微信头像
            $img = UserWeiXinAdd::where('user_id',$user_id)->first();
            if(empty($img)){
                $response = Lang::get("error.weixin_heading_img_error_10000");
                break;
            }
            if($type == 1){
                $storage_name = date('Ymdhis', time()).'.png';               //存储名称
                $file = Config::get('config.ahp_upload_files_url').$storage_name;
                $url = Config::get('config.ahp_upload_files');
                //把微信头像保存到服务器
               $res = Users::put_file_from_url_content($img->avatar,$storage_name,$url);
            }else{
                $storage_name = $img->avatar;
            }
            $User = Users::find($user_id);
            $User->heading_img = $storage_name;
            $User->updated_at = Common::currentTimeString();
            $User->save();

            $storage_name = [
                'storage_name' => $storage_name,
            ];
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $storage_name);
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "change_heading_img", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-08-16 14:59:29
     * @Description: 消息列表
     * 消息状态: 0-排队中, 1-发送中，2-发送完成，3-发送失败，4-消息被撤销，5-消息过期, 6-筛选结果为空，7-定时任务尚未开始处理
     * @link:
     */
    public function u_list(){
        $validator = Validator::make(Input::all(),
            array(
                'token' =>'required',
                'type' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $wechat_user=$this->verificationUserToken(Input::get("token"),$response);
            if(is_null($wechat_user)){
                break;
            }
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);

            $res = NewsLog::where('field_name','user_id')->where('field_value',$wechat_user->id)->orderBy('created_at','desc');
            if(Input::get('type')){
                $res->where('type',Input::get('type'));
            }
            $count = $res->count();
            $test = clone $res;
            $unread = $test->where('is_read',1)->count();
            $res=$res->skip($page_size*($page-1))->take($page_size)->get();

            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count, "unread"=> $unread, "u_list"=>$res));
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "u_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-08-18 15:59:29
     * @Description: 删除车辆 [弃用]
     * @link:
     */
    public function del_user_car_info(){
        $validator = Validator::make(Input::all(),
            array(
                'token' =>'required',
                'id' =>'required',  //user_car_info里面的id
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $wechat_user=$this->verificationUserToken(Input::get("token"),$response);
            if(is_null($wechat_user)){
                break;
            }

            //获取参数
            $id = Input::get('id');
            $user_car_info = UserCarInfo::where('id',$id)->where('user_id',$wechat_user->id)->first();
            if(empty($user_car_info)){
                $response = Lang::get("error.user_car_not_exit_error_1019");
                break;
            }
            if($user_car_info->status == 0){
                $response = Lang::get("error.car_user_info_del_error_11001");
                break;
            }
            try{
                UserCarInfo::where('id',$id)->where('user_id',$wechat_user->id)->update(array('status'=>0, 'updated_at'=>Common::currentTimeString()));
                UserCarInfoInquiry::where('user_car_info_id',$id)->where('user_id',$wechat_user->id)->update(array('status'=>0, 'updated_at'=>Common::currentTimeString()));

            }catch (Exception $e){
                Log::error("error", array("system" => "api3.0" ,"event" => "del_user_car_info", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("error.user_car_del_error_1009");
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok');
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "del_user_car_info", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-08-22 17:00:29
     * @Description: 消息详情
     * 消息状态: 0-排队中, 1-发送中，2-发送完成，3-发送失败，4-消息被撤销，5-消息过期, 6-筛选结果为空，7-定时任务尚未开始处理
     * @link: http://www.activity.com/wechat/api3.0/user/u_show?token=6905dd76f414168c185a28d027ea2156&id=282
     */
    public function u_show(){
        $validator = Validator::make(Input::all(),
            array(
                'token' =>'required',
                'id' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $wechat_user=$this->verificationUserToken(Input::get("token"),$response);
            if(is_null($wechat_user)){
                break;
            }
            $id=Input::get("id");

            $res = NewsLog::where('id',$id)->where('field_name','user_id')->where('field_value',$wechat_user->id)->first();
            if(is_null($res)){
                $response = Lang::get("error.u_meng_push_log_error_91001");
                break;
            }
            try{
                NewsLog::where('id',$id)->where('field_name','user_id')->where('field_value',$wechat_user->id)->where('is_read',1)->update(array(
                    'is_read' => 2,
                    'updated_at' => Common::currentTimeString(),
                    'received_at' => Common::currentTimeString(),
                ));
            }catch (Exception $e){
                Log::error("error", array("system" => "api3.0" ,"event" => "u_show", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("error.u_meng_push_log_error_91002");
                break;
            }

            $res->is_read = 2;
            //未读数
            $res->is_read_count = NewsLog::where('is_read',1)->where('field_name','user_id')->where('type',$res->type)->where('field_value',$wechat_user->id)->where('status',NewsLog::STATUS_NEW)->count();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $res);
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "u_show", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-08-28 14:36:29
     * @Description: 会员权益
     * @link:
     */
    public function getLegalRight(){
        $validator = Validator::make(Input::all(),
            array(
                'token' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $wechat_user=$this->verificationUserToken(Input::get("token"),$response);
            if(is_null($wechat_user)){
                break;
            }

            //获取用户会员料信息
            $member = MemberOrdinary::find($wechat_user->member);

            //获取所有会员等级
            $all_member = MemberOrdinary::where('status',1)->get();
            if(!is_null($all_member)){
                foreach($all_member as $key => $val){
                    $val->member_icon_url = Config::get('config.member_icon_url');
                }
            }

            //获取下一级会员等级
            $next_member = MemberOrdinary::where('experience','>',$member->experience)->where('status',1)->orderBy('created_at','asc')->first();
            $difference = '';
            if(!is_null($next_member)){
                $difference = $next_member->experience - $wechat_user->experience;
            }

            $result = array('member' => $member, 'next_member' => $next_member, 'all_member' => $all_member, 'difference' => $difference, 'user_experience' => $wechat_user->experience);
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "u_show", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-09-05 16:03:32
     * @Description: 分享
     */
    public function share(){
        $validator = Validator::make(Input::all(),
            array(
                "url"=>'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }

            $shareSet = ['title'=>Config::get('wechat3.default_share_title'),
                'url'=> Config::get('wechat3.default_share_url'),
                'image'=>Config::get('wechat3.default_share_img'),
                'desc'=>Config::get('wechat3.default_share_desc')];//    分享默认值
            $url = Input::get('url');
            $res = strpos($url,'from');
            if($res !== false){
                $temp_url = explode('?',$url);
                $url = $temp_url[0];
            }
            $wechat_share_data = WechatShareData::where('wechat_url',$url)->first();
            if(!is_null($wechat_share_data)){
                $shareSet['title'] = $wechat_share_data->tittle;
                $shareSet['url'] = Config::get('wechat3.redirect_url').$wechat_share_data->share_url;
                $shareSet['desc'] = $wechat_share_data->desc;
                $shareSet['image'] = Config::get('wechat3.share_img_url').$wechat_share_data->img_url;
            }

            $response = array('code' => 0, 'msg' => 'ok','shareSet' => $shareSet);
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "share", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

}